package tools;

/******************************************************************************
 *  Dataweb Project is a rapid application development.
 *  Copyright (C) 2004-2009 Danilo Limatola Esterno
 *
 *  This file is part of Dataweb Project.
 *
 *  Dataweb Project is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Dataweb Project is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Dataweb Project.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/
/**
 *
 * @authors
 * Danilo Limatola Esterno
 *
 */
import java.util.*;
import java.io.UnsupportedEncodingException;   
import java.net.URLEncoder;  

public class StringUtil {

    private StringUtil() {
    }

    public static String trim(String value) {
        value = rtrim(value);
        value = ltrim(value);
        return value;
    }

    public static String trim(String value, char[] c) {
        value = rtrim(value, c);
        value = ltrim(value, c);
        return value;
    }

    public static String trimAll(String value) {
        value = rtrim(value, new char[]{' ', '\n', '\r', '\t'});
        value = ltrim(value, new char[]{' ', '\n', '\r', '\t'});
        return value;
    }

    public static String rtrim(String value) {
        return rtrim(value, new char[]{' '});
    }

    public static String ltrim(String value) {
        return ltrim(value, new char[]{' '});
    }

    private static boolean isChar(char c, char[] chrs) {
        for (int i = 0; i <= chrs.length - 1; i++) {
            if (c == chrs[i]) {
                return true;
            }
        }
        return false;
    }

    public static String rtrim(String value, char[] c) {
        int v_length = value.length();
        String strTemp = "";

        if (v_length < 0) {
            return "";
        }
        int iTemp = v_length - 1;
        while (iTemp > -1) {
            if (isChar(value.charAt(iTemp), c)) {
            } else {
                strTemp = value.substring(0, iTemp + 1);
                break;
            }
            iTemp = iTemp - 1;
        }
        return strTemp;
    }

    public static String rtrimAll(String value) {
        return rtrim(value, new char[]{' ', '\n', '\r', '\t'});
    }

    public static String ltrim(String value, char[] c) {
        int v_length = value.length();
        String strTemp = "";

        if (v_length < 1) {
            return "";
        }
        int iTemp = 0;

        while (iTemp < v_length) {
            if (isChar(value.charAt(iTemp), c)) {
            } else {
                strTemp = value.substring(iTemp, v_length);
                break;
            }
            iTemp = iTemp + 1;
        }
        return strTemp;
    }

    public static String ltrimAll(String value) {
        return rtrim(value, new char[]{' ', '\n', '\r', '\t'});
    }

    public static String left(String v, int maxlen) {
        if (v.length() > maxlen) {
            return v.substring(0, maxlen);
        }
        return v;
    }

    public static String right(String v, int maxlen) {
        if (v.length() > maxlen) {
            return v.substring(v.length() - maxlen);
        }
        return v;
    }

    @Deprecated
    public static String addZero(int v, int maxlen) {
        return addZero(String.valueOf(v), maxlen);
    }

    @Deprecated
    public static String addZero(String v, int maxlen) {
        return prependZero(v, maxlen);
    }

    public static String prependZero(String v, int maxlen) {
        return prependChar(v, '0', maxlen);
    }

    public static String appendZero(String v, int maxlen) {
        return appendChar(v, '0', maxlen);
    }

    public static String prependSpace(String v, int maxlen) {
        return prependChar(v, ' ', maxlen);
    }

    public static String appendSpace(String v, int maxlen) {
        return appendChar(v, ' ', maxlen);
    }

    public static String prependChar(String v, char c, int maxlen) {
        String app = v;
        for (int i = 1; i <= maxlen - v.length(); i++) {
            app = c + app;
        }
        return app;
    }

    public static String appendChar(String v, char c, int maxlen) {
        String app = v;
        for (int i = 1; i <= maxlen - v.length(); i++) {
            app = app + c;
        }
        return app;
    }

    public static int checkName(String s) {
        return checkName(s, false);
    }

    public static int checkName(String s, boolean isNamespace) {
        if (s == null) {
            return -2;
        }
        if (s.equals("")) {
            return -2;
        }
        if (isNamespace) {
            String ar[] = s.split("\\.");
            if (ar.length == 1) {
                return -3;
            }
            s = s.replaceAll("\\.", "_");
        } else {
            //Verifica se inizia con il numero
            if ((s.charAt(0) >= '0') && (s.charAt(0) <= '9')) {
                return 0;
            }
        }
        String notAllow = "\\|!\"£$%&/*-+()=?'[]@#<>,.;:^{} ";
        for (int i = 0; i <= notAllow.length() - 1; i++) {
            char c = notAllow.charAt(i);
            int pos = s.indexOf(c);
            if (pos >= 0) {
                return pos;
            }
        }
        return -1;
    }

    public static String replaceText(String text, String find, String replace) {
        int p = 0;
        while (true) {
            p = text.indexOf(find, 0);
            if (p >= 0) {
                StringBuilder sb = new StringBuilder();
                sb.append(text.substring(0, p));
                sb.append(replace);
                sb.append(text.substring(p + find.length()));
                text = sb.toString();
            } else {
                break;
            }
        }
        return text;
    }

    public static String getResource(String key, Object[] prms, String fileRes,
            Locale locale) {
        ResourceBundle rb = getResourceBundle(fileRes, locale);
        String message = rb.getString(key);
        message = java.text.MessageFormat.format(message.replaceAll("'", "''"), prms);
        return message;
    }

    public static ResourceBundle getResourceBundle(String fileRes,
            Locale locale) {
        ResourceBundle rb = null;
        if (locale == null) {
            rb = ResourceBundle.getBundle(fileRes);
        } else {
            rb = ResourceBundle.getBundle(fileRes, locale); //Gestire la lingua
            if (!(rb.getLocale().getLanguage().equals(locale.getLanguage()))) {
                rb = ResourceBundle.getBundle(fileRes, new Locale(""));
            }
        }
        return rb;
    }
    public static String spcharToHTML(String str) {
    	str = str.replaceAll("&", "&amp;");
    	str = str.replaceAll("<", "&lt;");
    	str = str.replaceAll(">", "&gt;");
    	str = str.replaceAll("\"", "&quot;");
    	return str;
    }
    public static String encode(String str) throws UnsupportedEncodingException{   
        String isoStr = new String(str.getBytes("UTF8"), "ISO-8859-1");        
        char[] chars = isoStr.toCharArray();   
        StringBuffer sb = new StringBuffer();   
        for (int i = 0; i < chars.length; i++) {   
            if ((chars[i] <= 'z' && chars[i] >= 'a')   
                    || (chars[i] <= 'Z' && chars[i] >= 'A') || chars[i] == '-'  
                    || chars[i] == '_' || chars[i] == '.' || chars[i] == '!'  
                    || chars[i] == '~' || chars[i] == '*' || chars[i] == '\''  
                    || chars[i] == '(' || chars[i] == ')' || chars[i] == ';'  
                    || chars[i] == '/' || chars[i] == '?' || chars[i] == ':'  
                    || chars[i] == '@' || chars[i] == '&' || chars[i] == '='  
                    || chars[i] == '+' || chars[i] == '$' || chars[i] == ','  
                    || chars[i] == '#') {   
                sb.append(chars[i]);   
            } else {   
                sb.append("%");   
                sb.append(Integer.toHexString(chars[i]));   
            }   
        }   
        return sb.toString();   
    }   

}

